/*
 * ListAccelerator.java
 *
 * Created on September 22, 2007, 10:43 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package flightlifter;

import java.util.*;

/**
 *
 * @author Matt
 */
public class ListAccelerator extends Accelerator {

    Intersectable[] obs;

    /** Creates a new instance of ListAccelerator */
    public ListAccelerator() {
        objects = new Vector();
    }

    public void validate() {
        obs = new Intersectable[objects.size()];
        Intersectable temp;
        
        //copy objects into a fixed length array
        //so we can iterate over them faster when drawing
        for (int i = 0; i < objects.size(); i++) {
            temp = (Intersectable) objects.elementAt(i);
            obs[i] = temp;
        }
        objects = new Vector();
    }

    public void add(Intersectable newObject) {
        objects.add(newObject);

    }

    public void draw() {

        for (Intersectable i : obs) {
            i.draw();
        }

    }

    public boolean findNearest(Ray state) {

        float min = 1e6f;

        Intersectable obj, nearest = null;

        for (int i = 0; i < objects.size(); i++) {
            obj = (Intersectable) objects.elementAt(i);
            if (obj.intersect(state)) {

                if (state.distance < min) {
                    min = state.distance;
                    nearest = obj;

                }
            }
        }
        if (nearest == null) {
            return false;
        } else {
            state.distance = min;
            state.occluder = nearest;
            return true;
        }
    }
}
